package top.kent90.service;


import org.apache.dubbo.config.annotation.Service;
import org.springframework.beans.factory.annotation.Autowired;
import top.kent90.dao.ZczyAtegoryDao;
import top.kent90.pojo.ZczyAtegory;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

@Service
public class ZczyAtegoryServcieImpl implements ZczyAtegoryService {
    @Autowired
    ZczyAtegoryDao dao;

    //查询分类
    @Override
    public List<ZczyAtegory> queryByParentId(Integer parentId) {
        return dao.queryByParentId(parentId);
    }

    //增加分类
    @Override
    public int addAtegory(String pid, String name, String brief) {
        //创建分类对象
        ZczyAtegory zczyAtegory = new ZczyAtegory();
        if(null!=pid){
            zczyAtegory.setParentId(Integer.parseInt(pid));
        }else {
            zczyAtegory.setParentId(0);
        }
        zczyAtegory.setBrief(brief);
        zczyAtegory.setName(name);
        String creattime = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(new Date());
        zczyAtegory.setCreatetime(creattime);
        zczyAtegory.setCreateBy("");
        int rs = 0;
        try {
            rs = dao.addAtegory(zczyAtegory);
        } catch (Exception e) {
            //如果名字重复则出现异常，返回0 表示操作失败
            rs=0;
        }
        return rs;
    }
    //删除分类
    @Override
    public int delAtegory(String id) {
            int rs =0;

        if(null!=id){
            int categoryId =  Integer.parseInt(id);
            //判断是否该分类下是否有项目 结果为0表示没有 1 表示有
            Integer countAteItem = dao.countAteItem(categoryId);
            if(countAteItem==0){
            //删除分类
            rs = dao.delAtegory(categoryId);
            }
        }
        return rs;
    }

    @Override
    public int updAtegory(String categoryId, String name, String brief) {
        //创建分类对象
        ZczyAtegory zczyAtegory = new ZczyAtegory();
        int rs = 0;
        if(null!=categoryId) {
            System.out.println(categoryId+"++"+name+"++"+brief);
            zczyAtegory.setCategoryId(Integer.parseInt(categoryId));
            zczyAtegory.setBrief(brief);
            zczyAtegory.setName(name);
            String creattime = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(new Date());
            zczyAtegory.setCreatetime(creattime);
            zczyAtegory.setCreateBy("");

            try {
                rs = dao.updAtegory(zczyAtegory);
            } catch (Exception e) {
                //如果名字重复则出现异常，返回0 表示操作失败
                rs = 0;

            }
        }
        return rs;
    }
}
